package com.cabrida.livestar.util;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Constants;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 分享
 */
public class DialogShareView extends Dialog implements
		android.view.View.OnClickListener {
	private Dialog dialogShare;
	private LayoutInflater mInflater;
	private IWXAPI api;
	private static Activity mActivity;

	private static final int WX_FRIEND = 0;
	private static final int WX_CIRCLE = 1;
	private int wx_tag = WX_FRIEND;
	
	private String liveUrl="";
	private String liveTitle="";
	private String liveContent="";
	
	private EditText et_title;
	private EditText et_des;

	public DialogShareView(Context context) {
		super(context);
		mActivity = (Activity) context;
		api = WXAPIFactory.createWXAPI(mActivity, Constants.APP_ID, false);
	}

	public DialogShareView(Context context, int defStyle) {
		super(context, defStyle);
		mActivity = (Activity) context;
		api = WXAPIFactory.createWXAPI(mActivity, Constants.APP_ID, false);
	}

	public void initLiveDialog(String url,String title,String content) {
		liveUrl = url;
		liveTitle = title;
		liveContent = content;
		showLiveShareDialog(mActivity);
	}

	private void showLiveShareDialog(Activity activity) {

		initShareDialogView();

		if (null != dialogShare && dialogShare.isShowing()) {
			return;
		}

		if (null != dialogShare) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogShare.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = mActivity.getWindowManager().getDefaultDisplay().getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;

			// 设置显示位置
			dialogShare.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogShare.setCanceledOnTouchOutside(true);
			dialogShare.show();
		}
	}

	private void initShareDialogView() {

		mInflater = LayoutInflater.from(mActivity);

		View view = mInflater.inflate(R.layout.dialog_share_choice, null);

		TextView tvFriends = (TextView) view.findViewById(R.id.tv_friends);
		tvFriends.setOnClickListener(this);

		TextView tvFriendsCircle = (TextView) view.findViewById(R.id.tv_friends_circle);
		tvFriendsCircle.setOnClickListener(this);
		
		TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
		tv_msg.setOnClickListener(this);

		et_title = (EditText) view.findViewById(R.id.et_title);
		et_title.setText(liveTitle);
		et_des = (EditText) view.findViewById(R.id.et_des);
		et_des.setText(liveContent);
		
		Button btnCancle = (Button) view.findViewById(R.id.btn_cancel);
		btnCancle.setOnClickListener(this);

		dialogShare = new Dialog(mActivity, R.style.dialogWindowStyle);

		dialogShare.requestWindowFeature(Window.FEATURE_NO_TITLE);

		dialogShare.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

	}

	/** 点击分享 */
	private void share() {
		boolean result = api.registerApp(Constants.APP_ID);
		if (!result) {
			Toast.makeText(mActivity, "获取微信信息失败！", Toast.LENGTH_SHORT).show();
			return;
		}

		WXWebpageObject webpage = new WXWebpageObject();
		WXMediaMessage msg = new WXMediaMessage(webpage);

		liveUrl  = liveUrl+"&title="+liveTitle+"&content="+liveContent;
		webpage.webpageUrl = liveUrl;
		msg.description = liveContent;
		msg.title = liveTitle;

		Bitmap thumb = BitmapFactory.decodeResource(mActivity.getResources(),
				R.drawable.logo);
		msg.thumbData = WXUtil.bmpToByteArray(thumb, true);
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("webpage");
		req.message = msg;

		if (wx_tag == WX_FRIEND) {
			req.scene = SendMessageToWX.Req.WXSceneSession;
		} else if (wx_tag == WX_CIRCLE) {
			req.scene = SendMessageToWX.Req.WXSceneTimeline;
		}

		api.sendReq(req);
	}
	
	/**
	 * 调起系统发短信功能
	 * @param phoneNumber
	 * @param message
	 */
	public void doSendSMSTo(String phoneNumber,String message){
		liveUrl  = liveUrl+"&title="+liveTitle+"&content="+liveContent;
		
//		if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){
			Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));          
			intent.putExtra("sms_body", message+","+"观看连接"+liveUrl);
			mActivity.startActivity(intent);
//		}
	}


	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_friends:
		{
			wx_tag = WX_FRIEND;
			String title  = et_title.getText().toString().trim();
			String content  = et_des.getText().toString().trim();
			if (!TextUtils.isEmpty(title)) {
				liveTitle = title;
			}
			if (!TextUtils.isEmpty(content)) {
				liveContent = content;
			}
			share();
			if (null != dialogShare) {
				dialogShare.dismiss();
			}
		}
			break;
		case R.id.tv_friends_circle:
		{
			wx_tag = WX_CIRCLE;
			String title  = et_title.getText().toString().trim();
			String content  = et_des.getText().toString().trim();
			if (!TextUtils.isEmpty(title)) {
				liveTitle = title;
			}
			if (!TextUtils.isEmpty(content)) {
				liveContent = content;
			}
			share();
			if (null != dialogShare) {
				dialogShare.dismiss();
			}
		}
			break;
		case R.id.tv_msg:
		{
			String title  = et_title.getText().toString().trim();
			String content  = et_des.getText().toString().trim();
			if (!TextUtils.isEmpty(title)) {
				liveTitle = title;
			}
			if (!TextUtils.isEmpty(content)) {
				liveContent = content;
			}
			doSendSMSTo("",liveTitle+liveContent);
			if (null != dialogShare) {
				dialogShare.dismiss();
			}
		}
		break;
		case R.id.btn_cancel:
			if (null != dialogShare) {
				dialogShare.dismiss();
			}
			break;

		default:
			break;
		}
	}

}
